<template>
	<view>
		<nav-custom bgColor="bg-gradual-pink" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">订单确认</block>
		</nav-custom>
		<view class="content">
			<view class="row">
				<view class="nominal">
					姓名
				</view>
				<view class="input">
					<input placeholder="请输入你的姓名" type="text" v-model="contact" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					联系电话
				</view>
				<view class="input">
					<input placeholder="请输入你的手机号码" type="number" v-model="telphone" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					用车日期
				</view>
				<view class="input">
					<picker mode="date" :value="colectionDate" :start="startDate" :end="endDate" @change="bindDateChange">
						<view class="time-input">{{colectionDate}}</view>
					</picker>
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					用车时间
				</view>
				<view class="input">
					<picker mode="time" :value="collectionTime" start="09:01" end="21:01" @change="bindTimeChange">
						<view class="time-input">{{collectionTime}}</view>
					</picker>
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					用车地点
				</view>
				<view class="input">
					<textarea v-model="collectionAddress" auto-height="true" placeholder="输入用车地点"></textarea>
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					留言
				</view>
				<view class="input">
					<textarea v-model="message" auto-height="true" placeholder="选填"></textarea>
				</view>
			</view>
		</view>
		<!-- 购买商品列表 -->
		<view class="buy-list">
			<view class="row" v-for="(row,index) in buylist" :key="index">
				<view class="goods-info">
					<view class="img">
						<image :src="row.image"></image>
					</view>
					<view class="info">
						<view class="title">{{row.targetName}}</view>
						<view class="spec">{{row.selectedHour}}小时 {{row.selectedKilo}}公里</view>
						<!-- <view class="time">用车时间:{{row.targetTime}}</view> -->
						<view class="price-number">
							<view class="price">￥{{row.jieqinPrice*row.targetNum}}</view>
							<view class="number">
								x{{row.targetNum}}辆
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 明细 -->
		<view class="detail">
			<view class="row">
				<view class="nominal">
					市场价
				</view>
				<view class="price">
					￥{{goodsPrice|toFixed}}
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					结亲价
				</view>
				<view class="price">
					￥{{sumPrice|toFixed}}
				</view>
			</view>
		</view>
		<view class="blck"></view>
		<view class="footer">
			<view class="settlement">
				<view class="sum">总金额:<view class="money">￥{{sumPrice|toFixed}}</view>
				</view>
				<view class="btn" @tap="toPay">提交订单</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				buylist: [], //订单列表
				goodsPrice: 0.0, //商品合计价格
				sumPrice: 0.0, //用户付款价格
				colectionDate: '请输入用车日期', //集合日期
				collectionAddress: '', //集合地点
				collectionAddressType: 0, //集合类型
				collectionTime: '请输入用车时间', //集合时间
				contact: '', //联系人
				message: '', //留言
				telphone: this.$store.state.userInfo.telphone, //电话

				startDate: this.getData(),
				endDate: this.getData(1),
			};
		},
		onShow() {
			//页面显示时，加载订单信息
			uni.getStorage({
				key: 'buylist',
				success: (ret) => {
					this.buylist = ret.data;
					[this.goodsPrice, this.sumPrice] = [0, 0];
					//合计
					let len = this.buylist.length;
					for (let i = 0; i < len; i++) {
						this.goodsPrice = this.goodsPrice + (this.buylist[i].targetNum * this.buylist[i].earnestPrice);
						this.sumPrice = this.sumPrice + (this.buylist[i].targetNum * this.buylist[i].jieqinPrice);
					}
				}
			});
		},
		onHide() {

		},
		onBackPress() {
			//页面后退时候，清除订单信息
			this.clearOrder();
		},
		filters: {
			toFixed: function(x) {
				return parseFloat(x).toFixed(2);
			}
		},
		methods: {
			/**
			 * 选择日期
			 */
			bindDateChange(e) {
				this.colectionDate = e.target.value
			},
			/**
			 * 现在时间
			 */
			bindTimeChange(e) {
				this.collectionTime = e.target.value
			},
			/**
			 * 获取日期
			 */
			getData(end) {
				let dd = new Date()
				let y = dd.getFullYear()
				let m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
				let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
				return (end ? (y + 2) : y) + '-' + m + '-' + d
			},
			/**
			 * 清除订单
			 */
			clearOrder() {
				uni.removeStorage({
					key: 'buylist',
					success: (res) => {
						this.buylist = [];
					}
				});
			},
			/**
			 * 提交
			 */
			toPay() {
				if (this.contact == '') {
					uni.showToast({
						title: '请填写姓名',
						icon: 'none'
					});
					return;
				}
				if (!(/^1[0-9]{10}$/.test(this.telphone))) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的手机号'
					});
					return
				}
				if (this.colectionDate == '') {
					uni.showToast({
						title: '请填写用车日期',
						icon: 'none'
					});
					return;
				}
				if (this.collectionTime == '') {
					uni.showToast({
						title: '请填写用车时间',
						icon: 'none'
					});
					return;
				}
				if (this.collectionAddress == '') {
					uni.showToast({
						title: '请填写集合地点',
						icon: 'none'
					});
					return;
				}
				let carOrderDetailList = [];
				let len = this.buylist.length;
				for (let i = 0; i < len; i++) {
					let obj = {
						carNum: this.buylist[i].typeId == 2 ? `${this.buylist[i].targetNum}_${this.buylist[i].normalNum}` : this.buylist[
							i].targetNum,
						earnestPrice: this.buylist[i].earnestPrice,
						// isMainCar: this.buylist[i].isMainCar,
						jieqinPrice: this.buylist[i].jieqinPrice,
						selectRequired: `${this.buylist[i].selectedHour}_${this.buylist[i].selectedKilo}`,
						targetId: this.buylist[i].targetId,
						targetType: this.buylist[i].typeId
					}
					carOrderDetailList.push(obj);
				}
				if (carOrderDetailList.length == 0) {
					uni.showToast({
						title: '订单信息有误，请重新购买',
						icon: 'none'
					});
					return;
				}
				//本地模拟订单提交UI效果
				uni.showLoading({
					title: '正在提交订单...'
				})
				let data = {
					carOrderDetailList: JSON.stringify(carOrderDetailList),
					colectionDate: this.colectionDate, //集合日期
					collectionAddress: this.collectionAddress, //集合地点
					collectionAddressType: this.collectionAddressType, //集合类型
					collectionTime: this.collectionTime, //集合时间
					contact: this.contact, //联系人
					telphone: this.telphone, //电话
					message: this.message,
					countyId: this.$store.state.cityId
				}
				this.$api.OrderSubmit(data).then(res => {
					uni.hideLoading();
					uni.showToast({
						title: '婚车下单成功,稍后客服会联系您',
						icon: 'none'
					});
					uni.navigateTo({
						url: 'success'
					})
				});
			},
		}
	}
</script>

<style lang="scss">
	.content {
		width: calc(100% - 60upx);
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		background-color: #fff;
		display: flex;
		flex-wrap: wrap;

		view {
			display: flex;
		}

		.row {
			width: 94%;
			padding: 0 20upx;
			margin: 0 3%;
			border-top: solid 1upx #eee;

			&:first-child {
				border-top: none;
			}

			.nominal {
				width: 30%;
				height: 100upx;
				font-weight: 200;
				font-size: 30upx;
				align-items: center;
			}

			.input {
				width: 70%;
				padding: 20upx 0;
				align-items: center;
				font-size: 30upx;

				&.switch {
					justify-content: flex-end;
				}

				.textarea {
					margin: 20upx 0;
					min-height: 120upx;
				}
			}

			.del {
				width: 100%;
				height: 100upx;
				justify-content: center;
				align-items: center;
				font-size: 36upx;
				color: #f06c7a;
				background-color: rgba(255, 0, 0, 0.05);
				border-bottom: solid 1upx #eee;
			}
		}
	}

	.buy-list {
		width: calc(100% - 60upx);
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			margin: 30upx 0;

			.goods-info {
				width: 100%;
				display: flex;

				.img {
					width: 22vw;
					height: 22vw;
					border-radius: 10upx;
					overflow: hidden;
					flex-shrink: 0;
					margin-right: 10upx;

					image {
						width: 22vw;
						height: 22vw;
					}
				}

				.info {
					width: 100%;
					height: 22vw;
					overflow: hidden;
					display: flex;
					flex-wrap: wrap;
					position: relative;

					.title {
						width: 100%;
						font-size: 28upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						// text-align: justify;
						overflow: hidden;
					}

					.spec {
						font-size: 22upx;
						background-color: #f3f3f3;
						color: #a7a7a7;
						height: 40upx;
						display: flex;
						align-items: center;
						padding: 0 20upx;
						margin-bottom: 30upx;
						border-radius: 20upx;
					}
					
					.time{
						font-size: 22upx;
						color: #a7a7a7;
						height: 40upx;
					}

					.price-number {
						position: absolute;
						width: 100%;
						bottom: 0upx;
						display: flex;
						justify-content: space-between;
						align-items: flex-end;
						font-size: 28upx;
						height: 40upx;

						.price {
							color: #f06c7a;
						}

						.number {
							display: flex;
							justify-content: center;
							align-items: center;

						}
					}
				}
			}
		}
	}

	.order {
		width: calc(100% - 60upx);
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			margin: 20upx 0;
			height: 40upx;
			display: flex;

			.left {
				font-size: 28upx;
			}

			.right {
				margin-left: 40upx;
				font-size: 26upx;
				color: #999;

				input {
					font-size: 26upx;
					color: #999;
				}
			}
		}
	}

	.blck {
		width: 100%;
		height: 100upx;
	}

	.footer {
		width: 100%;
		padding: 0 4%;
		background-color: #fbfbfb;
		height: 100upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 28upx;
		position: fixed;
		bottom: 0upx;
		z-index: 5;
		border-top: 1upx solid #e7e7e7;

		.settlement {
			width: 80%;
			display: flex;
			justify-content: flex-end;
			align-items: center;

			.sum {
				width: 50%;
				font-size: 28upx;
				margin-right: 10upx;
				display: flex;
				justify-content: flex-end;

				.money {
					font-weight: 600;
				}
			}

			.btn {
				padding: 0 30upx;
				height: 60upx;
				background-color: #f06c7a;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30upx;
				border-radius: 40upx;
			}
		}
	}

	.detail {
		width: calc(100% - 60upx);
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			height: 60upx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.nominal {
				font-size: 28upx;
			}

			.price {
				font-size: 26upx;
				color: #f06c7a;
			}
		}
	}
</style>
